package com.genesis.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.genesis.mall.common.Result;
import com.genesis.mall.dto.OmsOrderReturnApplyDto;
import com.genesis.mall.dto.OrderReturnApply;
import com.genesis.mall.dto.PageDto;
import com.genesis.mall.entity.OmsCompanyAddress;
import com.genesis.mall.entity.OmsOrder;
import com.genesis.mall.entity.OmsOrderReturnApply;
import com.genesis.mall.mapper.OmsOrderReturnApplyMapper;
import com.genesis.mall.service.IOmsCompanyAddressService;
import com.genesis.mall.service.IOmsOrderReturnApplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 订单退货申请 服务实现类
 * </p>
 *
 * @author author
 * @since 2022-08-01
 */
@Slf4j
@Service
public class OmsOrderReturnApplyServiceImpl extends ServiceImpl<OmsOrderReturnApplyMapper, OmsOrderReturnApply> implements IOmsOrderReturnApplyService {

    @Autowired
    private IOmsCompanyAddressService companyAddressService;

    @Override
    public boolean deleteByIds(List<Long> ids) {
        //遍历ids 获取退货申请对象
        for (Long id : ids) {
            OmsOrderReturnApply omsOrderReturnApply = getById(id);
            if (omsOrderReturnApply.getStatus()!=3){
                return false;
            }
            removeById(id);
        }

        return true;
    }

    @Override
    public PageDto<OmsOrderReturnApply> getList(OrderReturnApply orderReturnApply, Integer pageNum, Integer pageSize) {

        Page<OmsOrderReturnApply> omsOrderReturnApplyPage = new Page<>(pageNum, pageSize);

        LambdaQueryWrapper<OmsOrderReturnApply> lqw = new LambdaQueryWrapper<>();
        //申请时间

        if (orderReturnApply.getCreateTime()!=null){
            String start= orderReturnApply.getCreateTime()+" 00:00:00";
            String  end= orderReturnApply.getCreateTime()+" 23:59:59";

            LocalDateTime startTime = LocalDateTime.parse(start,
                    DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

            LocalDateTime endTime = LocalDateTime.parse(end,
                    DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                    lqw.between(OmsOrderReturnApply::getCreateTime, startTime, endTime);

            }
        //处理人员
        if (StringUtils.isNotEmpty(orderReturnApply.getHandleMan())){
            lqw.eq(OmsOrderReturnApply::getHandleMan,orderReturnApply.getHandleMan());
        }
        //处理时间
        if (orderReturnApply.getHandleTime()!=null){
            LocalDateTime startTime = LocalDateTime.parse(orderReturnApply.getHandleTime() +" "+"00:00:00",
                    DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

            LocalDateTime endTime = LocalDateTime.parse(orderReturnApply.getHandleTime()+" " + "23:59:59",
                    DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

                lqw.between(OmsOrderReturnApply::getHandleTime,startTime,endTime);

        }
        //服务id
        if (orderReturnApply.getId()!=null){
            lqw.eq(OmsOrderReturnApply::getId,orderReturnApply.getId());
        }
        //状态
        if (orderReturnApply.getStatus()!=null){
            lqw.eq(OmsOrderReturnApply::getStatus,orderReturnApply.getStatus());
        }

        page(omsOrderReturnApplyPage, lqw);
        PageDto<OmsOrderReturnApply> omsOrderReturnApplyPageDto = new PageDto<>();
        omsOrderReturnApplyPageDto.setPageNum(pageNum);
        omsOrderReturnApplyPageDto.setPageSize(pageSize);
        omsOrderReturnApplyPageDto.setTotal((int) omsOrderReturnApplyPage.getTotal());
        omsOrderReturnApplyPageDto.setTotalPage((int) omsOrderReturnApplyPage.getPages());
        omsOrderReturnApplyPageDto.setList(omsOrderReturnApplyPage.getRecords());
        return omsOrderReturnApplyPageDto;


    }

    @Override
    public void updateStatus(Long id, OmsOrderReturnApply omsOrderReturnApply) {

        //确认时间
        if (omsOrderReturnApply.getStatus()==2){
            omsOrderReturnApply.setReceiveTime(LocalDateTime.now());
        }

        if (omsOrderReturnApply.getStatus()==1 ||omsOrderReturnApply.getStatus()==3){
            omsOrderReturnApply.setHandleTime(LocalDateTime.now());
        }


        LambdaQueryWrapper<OmsOrderReturnApply> lqw = new LambdaQueryWrapper<>();
        lqw.eq(OmsOrderReturnApply::getId,id);
        update(omsOrderReturnApply,lqw);
    }

    @Override
    public OmsOrderReturnApplyDto getInfo(Long id) {
        OmsOrderReturnApply omsOrderReturnApply = getById(id);
        OmsCompanyAddress companyAddress = companyAddressService.getById(omsOrderReturnApply.getCompanyAddressId());
        OmsOrderReturnApplyDto omsOrderReturnApplyDto = new OmsOrderReturnApplyDto();
        BeanUtils.copyProperties(omsOrderReturnApply,omsOrderReturnApplyDto);
        omsOrderReturnApplyDto.setCompanyAddress(companyAddress);
        return omsOrderReturnApplyDto;
    }
}
